package cn.autumnorange.app.common.repository.impl;

import cn.autumnorange.app.common.entity.BaseEntity;
import cn.autumnorange.app.common.repository.BaseRepository;

import java.util.HashMap;
import java.util.Map;

public abstract class BaseRepositoryImpl<X extends BaseEntity>
    extends AbstractRepositoryImpl<X, Long> implements BaseRepository<X> {
  @Override
  public void softDelete(X entity) {
    if (!entity.getIsDelete()) {
      entity.setIsDelete(true);
    }
    super.update(entity);
  }

  @Override
  public boolean softDeleteById(Long id) {
    Map conditionParams = new HashMap();
    conditionParams.put("id", id);
    if (updateFileds(getClazz(), "as e set e.isDelete=true where e.id=:id", conditionParams, null) > 0)
      return true;
    return false;
  }

  @Override
  public X saveOrUpdate(X entity) {
    if (entity.getId() != null) {
      save(entity);
    } else {
      update(entity);
    }
    return entity;
  }
}
